<!-- 商品仓库配置 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row type="flex" justify="space-between" :gutter="48">
                    <a-col :md="16">
                    </a-col>
                    <a-col :md="8">
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" size="small">
            <div slot="status" slot-scope="status">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="status == 1 ? true : false" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <a-button type="link" @click="operation(record,'1')">上单表</a-button>
                <a-button type="link" @click="operation(record,'0')">下单表</a-button>
            </div>
            <div slot="zone" slot-scope="text,record">
                <a-button type="link" @click="goNext(record,'0')">商品区</a-button>
                <a-button type="link" @click="goNext(record,'1')">活动区</a-button>
            </div>
        </a-table>
    </div>
</template>

<script>
import { columns } from './table';
import { queryGoodsStorehouseBindConfigByUserid } from '@/api/newMall';
export default {
    data () {
        return {
            brandId: this.global.brandId,
            brandList: [],
            dataList: [],
            total: 0,
            page: 1,
            size: 10,
            item: {
                id: '',
                storehouseId: '', // 仓库ID
                userId: '', // 用户ID
                status: '',
            },
            confirmLoading: false,
            isload: false,
            visible: false,
            columns,
            drawerVisible: false,
            colors: ['blue','purple','cyan','green','orange','red','pink','brown','indigo','lime','purple','teal','yellow','amber'],
            wareHouseList: [],
            userId: sessionStorage.getItem('userId'),
            roles: this.$store.getters.roles,
        };
    },
    async created(){
        try {
            this.getData()
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: '网路丢失，请稍后再试！',
                duration: 8,
            })
        }
    },
    methods: {
        // 查询所有仓库
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'userId': _this.userId,
            }
            queryGoodsStorehouseBindConfigByUserid(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data
                    _this.total = res.data.length
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
        },
        // 添加
        add(){
            if(!this.brandId){
                this.$message.warning('请先选择品牌')
                return
            }
            this.item.id = ''
            this.item.storehouseId = undefined
            this.item.userId = ''
            this.item.brandId = this.brandId
            this.item.status = '1'
            this.item.type = undefined
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.visible = true
        },
        // 确定添加、修改
        handleOk(){
            let _this = this
            if(_this.item.storehouseId === undefined){
                this.$message.warning('请选择绑定仓库');
                return
            }
            if(_this.item.userId == ''){
                this.$message.warning('请输入用户ID');
                return
            }
            const params = {
                'id':_this.item.id,
                'brandId':_this.item.brandId,
                'storehouseId':_this.item.storehouseId,
                'userId':_this.item.userId,
                'status':_this.item.status,
            }
            updateGoodsStorehouseBindConfig(params)
            .then(res=>{
                _this.confirmLoading = false
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id == '' ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                    _this.visible = false
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 查看分区
        goNext(item,type){
            localStorage.setItem('storehouseId',JSON.stringify(item.storehouseId))
            localStorage.setItem('storehouseName',JSON.stringify(item.storehouseName))
            if(type == '0'){
                localStorage.setItem('zoneItem',JSON.stringify({'id': '0','name': '商品区'}))
                // this.$router.push({ path: '/newMall/classify',})
                this.$router.push({ path: '/newMall/goodLists',})
            }else{
                this.$router.push({ path: '/newMall/zone',})
            }
        },
        // 跳转上下单
        operation(item,type){
            localStorage.setItem('storehouseId',JSON.stringify(item.storehouseId))
            localStorage.setItem('storehouseName',JSON.stringify(item.storehouseName))
            // if(type == '0'){
            //     this.$router.push({ path: '/newMall/purchase', query: {type: type}})
            // }else{
            //     this.$router.push({ path: '/newMall/purchaseOrderStatisDate',query: {type: type}})
            // }
            this.$router.push({ path: '/newMall/purchaseOrderStatisDate',query: {type: type}})
        },
    }
}

</script>
<style scoped>
td{
    width: 140px;
    height: 30px;
}
.green{
    color: #87d068;
}
</style>